package com.bhkj.pdjhforotherapp.core.printutilspdj;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import com.blankj.utilcode.util.ToastUtils;
import com.printsdk.usbsdk.UsbDriver;

import static com.bhkj.pdjhforotherapp.core.printutilspdj.UsbPrintManager.PID11;
import static com.bhkj.pdjhforotherapp.core.printutilspdj.UsbPrintManager.PID13;
import static com.bhkj.pdjhforotherapp.core.printutilspdj.UsbPrintManager.PID15;
import static com.bhkj.pdjhforotherapp.core.printutilspdj.UsbPrintManager.VENDORID;
import static com.bhkj.pdjhforotherapp.core.printutilspjq.Constant.ACTION_USB_PERMISSION;

/*
 *  BroadcastReceiver when insert/remove the device USB plug into/from a USB port
 *  创建一个广播接收器接收USB插拔信息：当插入USB插头插到一个USB端口，或从一个USB端口，移除装置的USB插头
 */
public class UsbReceiver extends BroadcastReceiver {
    UsbDriver mUsbDriver;
    UsbDevice mUsbDev1;        //打印机1
    UsbDevice mUsbDev2;        //打印机2
    UsbDevice mUsbDev;

    public UsbReceiver(UsbDriver usbDriver) {
        this.mUsbDriver = usbDriver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            if (mUsbDriver.usbAttached(intent)) {
                UsbDevice device = intent
                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if ((device.getProductId() == PID11 && device.getVendorId() == VENDORID)
                        || (device.getProductId() == PID13 && device.getVendorId() == VENDORID)
                        || (device.getProductId() == PID15 && device.getVendorId() == VENDORID)) {
                    if (mUsbDriver.openUsbDevice(device)) {
                        if (device.getProductId() == PID11) {
                            mUsbDev1 = device;
                            mUsbDev = mUsbDev1;
                        } else {
                            mUsbDev2 = device;
                            mUsbDev = mUsbDev2;
                        }
                    }
                }
            }
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            UsbDevice device = intent
                    .getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if ((device.getProductId() == PID11 && device.getVendorId() == VENDORID)
                    || (device.getProductId() == PID13 && device.getVendorId() == VENDORID)
                    || (device.getProductId() == PID15 && device.getVendorId() == VENDORID)) {
                mUsbDriver.closeUsbDevice(device);
                if (device.getProductId() == PID11) {
                    mUsbDev1 = null;
                } else {
                    mUsbDev2 = null;
                }
            }
        } else if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if ((device.getProductId() == PID11 && device.getVendorId() == VENDORID)
                            || (device.getProductId() == PID13 && device.getVendorId() == VENDORID)
                            || (device.getProductId() == PID15 && device.getVendorId() == VENDORID)) {
                        if (mUsbDriver.openUsbDevice(device)) {
                            if (device.getProductId() == PID11) {
                                mUsbDev1 = device;
                                mUsbDev = mUsbDev1;
                            } else {
                                mUsbDev2 = device;
                                mUsbDev = mUsbDev2;
                            }
                        }
                    }
                } else {
                    ToastUtils.showShort("当前USB打印机拒绝授权");
                    //Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
}
